<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
	<title>range(范围)数据类型 | ElasticSearch 7.7 权威指南中文版</title>
	<meta name="keywords" content="ElasticSearch 权威指南中文版, elasticsearch 7, es7, 实时数据分析，实时数据检索" />
    <meta name="description" content="ElasticSearch 权威指南中文版, elasticsearch 7, es7, 实时数据分析，实时数据检索" />
    <!-- Give IE8 a fighting chance -->
    <!--[if lt IE 9]>
    <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
    <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
	<link rel="stylesheet" type="text/css" href="../static/styles.css" />
	<script>
	var _link = 'range.html';
    </script>
</head>
<body>
<div class="main-container">
    <section id="content">
        <div class="content-wrapper">
            <section id="guide" lang="zh_cn">
                <div class="container">
                    <div class="row">
                        <div class="col-xs-12 col-sm-8 col-md-8 guide-section">
                            <div style="color:gray; word-break: break-all; font-size:12px;">原英文版地址: <a href="https://www.elastic.co/guide/en/elasticsearch/reference/7.7/range.html" rel="nofollow" target="_blank">https://www.elastic.co/guide/en/elasticsearch/reference/7.7/range.html</a>, 原文档版权归 www.elastic.co 所有<br/>本地英文版地址: <a href="../en/range.html" rel="nofollow" target="_blank">../en/range.html</a></div>
                        <!-- start body -->
                  <div class="page_header">
<strong>重要</strong>: 此版本不会发布额外的bug修复或文档更新。最新信息请参考 <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/index.html" rel="nofollow">当前版本文档</a>。
</div>
<div id="content">
<div class="breadcrumbs">
<span class="breadcrumb-link"><a href="index.html">Elasticsearch 权威指南 [7.7]</a></span>
»
<span class="breadcrumb-link"><a href="mapping.html">映射</a></span>
»
<span class="breadcrumb-link"><a href="mapping-types.html">字段数据类型</a></span>
»
<span class="breadcrumb-node">range(范围)数据类型</span>
</div>
<div class="navheader">
<span class="prev">
<a href="percolator.html">« percolator(过滤器)数据类型</a>
</span>
<span class="next">
<a href="rank-feature.html">rank_feature(特征排名)数据类型 »</a>
</span>
</div>
<div class="section">
<div class="titlepage"><div><div>
<h2 class="title">
<a id="range"></a>range(范围)数据类型
</h2>
</div></div></div>

<p>支持以下几种范围类型：</p>
<div class="informaltable">
<table border="0" cellpadding="4px">
<colgroup>
<col>
<col>
</colgroup>
<tbody valign="top">
<tr>
<td valign="top">
<p>
<code class="literal">integer_range</code>
</p>
</td>
<td valign="top">
<p>
32位整数范围，最小值为<code class="literal">-2<sup>31</sup></code>，最大值为<code class="literal">2<sup>31</sup>-1</code>。
</p>
</td>
</tr>
<tr>
<td valign="top">
<p>
<code class="literal">float_range</code>
</p>
</td>
<td valign="top">
<p>
单精度32位 IEEE 754 浮点值范围。
</p>
</td>
</tr>
<tr>
<td valign="top">
<p>
<code class="literal">long_range</code>
</p>
</td>
<td valign="top">
<p>
64位有符号整数，最小值为<code class="literal">-2<sup>63</sup></code>，最大值为<code class="literal">2<sup>63</sup>-1</code>。
</p>
</td>
</tr>
<tr>
<td valign="top">
<p>
<code class="literal">double_range</code>
</p>
</td>
<td valign="top">
<p>
64位双精度 IEEE 754 浮点值范围。
</p>
</td>
</tr>
<tr>
<td valign="top">
<p>
<code class="literal">date_range</code>
</p>
</td>
<td valign="top">
<p>
自系统纪元以来，以无符号64位整数毫秒表示的日期值范围。
</p>
</td>
</tr>
<tr>
<td valign="top">
<p>
<code class="literal">ip_range</code>
</p>
</td>
<td valign="top">
<p>
支持<a href="https://en.wikipedia.org/wiki/IPv4" class="ulink" target="_top">IPv4</a>或<a href="https://en.wikipedia.org/wiki/IPv6" class="ulink" target="_top">IPv6</a>(或混合)地址的IP值范围。
</p>
</td>
</tr>
</tbody>
</table>
</div>
<p>下面是一个使用各种范围字段配置映射的示例，后面是一个索引多个范围类型的示例。</p>
<div class="pre_wrapper lang-console">
<pre class="programlisting prettyprint lang-console">PUT range_index
{
  "settings": {
    "number_of_shards": 2
  },
  "mappings": {
    "properties": {
      "expected_attendees": {
        "type": "integer_range"
      },
      "time_frame": {
        "type": "date_range", <a id="CO318-1"></a><i class="conum" data-value="1"></i>
        "format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"
      }
    }
  }
}

PUT range_index/_doc/1?refresh
{
  "expected_attendees" : { <a id="CO318-2"></a><i class="conum" data-value="2"></i>
    "gte" : 10,
    "lte" : 20
  },
  "time_frame" : { <a id="CO318-3"></a><i class="conum" data-value="3"></i>
    "gte" : "2015-10-31 12:00:00", <a id="CO318-4"></a><i class="conum" data-value="4"></i>
    "lte" : "2015-11-01"
  }
}</pre>
</div>
<div class="console_widget" data-snippet="snippets/692.console"></div>
<div class="calloutlist">
<table border="0" summary="Callout list">
<tr>
<td align="left" valign="top" width="5%">
<p><a href="#CO318-1"><i class="conum" data-value="1"></i></a></p>
</td>
<td align="left" valign="top">
<p><code class="literal">date_range</code>类型接受由<a class="xref" href="date.html" title="date 数据类型"><code class="literal">date</code></a>类型定义的相同的字段参数。</p>
</td>
</tr>
<tr>
<td align="left" valign="top" width="5%">
<p><a href="#CO318-2"><i class="conum" data-value="2"></i></a></p>
</td>
<td align="left" valign="top">
<p>为10~20名与会者的会议编制索引的示例。</p>
</td>
</tr>
<tr>
<td align="left" valign="top" width="5%">
<p><a href="#CO318-3"><i class="conum" data-value="3"></i></a></p>
</td>
<td align="left" valign="top">
<p>日期范围接受与<a class="xref" href="query-dsl-range-query.html#ranges-on-dates" title="在日期字段是执行范围查询">日期范围查询</a>中所述相同的格式。</p>
</td>
</tr>
<tr>
<td align="left" valign="top" width="5%">
<p><a href="#CO318-4"><i class="conum" data-value="4"></i></a></p>
</td>
<td align="left" valign="top">
<p>
使用日期时间戳的日期范围示例。

还可以接受<a class="xref" href="common-options.html#date-math" title="Date Math" rel="nofollow">date math</a>格式。

请注意，在索引时不能使用“now”。
</p>
</td>
</tr>
</table>
</div>
<p>以下是对名为“expected_attendees”的<code class="word">integer_range</code>字段进行<a class="xref" href="query-dsl-term-query.html" title="term 查询">term 查询</a>的示例。</p>
<div class="pre_wrapper lang-console">
<pre class="programlisting prettyprint lang-console">GET range_index/_search
{
  "query" : {
    "term" : {
      "expected_attendees" : {
        "value": 12
      }
    }
  }
}</pre>
</div>
<div class="console_widget" data-snippet="snippets/693.console"></div>
<p>上述查询产生的结果：</p>
<div class="pre_wrapper lang-console-result">
<pre class="programlisting prettyprint lang-console-result">{
  "took": 13,
  "timed_out": false,
  "_shards" : {
    "total": 2,
    "successful": 2,
    "skipped" : 0,
    "failed": 0
  },
  "hits" : {
    "total" : {
        "value": 1,
        "relation": "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "range_index",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 1.0,
        "_source" : {
          "expected_attendees" : {
            "gte" : 10, "lte" : 20
          },
          "time_frame" : {
            "gte" : "2015-10-31 12:00:00", "lte" : "2015-11-01"
          }
        }
      }
    ]
  }
}</pre>
</div>
<p>下面是对名为“time_frame”的<code class="literal">date_range</code>字段进行<code class="literal">date_range</code>查询的示例：</p>
<div class="pre_wrapper lang-console">
<pre class="programlisting prettyprint lang-console">GET range_index/_search
{
  "query" : {
    "range" : {
      "time_frame" : { <a id="CO319-1"></a><i class="conum" data-value="1"></i>
        "gte" : "2015-10-31",
        "lte" : "2015-11-01",
        "relation" : "within" <a id="CO319-2"></a><i class="conum" data-value="2"></i>
      }
    }
  }
}</pre>
</div>
<div class="console_widget" data-snippet="snippets/694.console"></div>
<div class="calloutlist">
<table border="0" summary="Callout list">
<tr>
<td align="left" valign="top" width="5%">
<p><a href="#CO319-1"><i class="conum" data-value="1"></i></a></p>
</td>
<td align="left" valign="top">
<p>范围查询的工作方式与<a class="xref" href="query-dsl-range-query.html" title="范围查询">范围查询</a>中描述的相同。</p>
</td>
</tr>
<tr>
<td align="left" valign="top" width="5%">
<p><a href="#CO319-2"><i class="conum" data-value="2"></i></a></p>
</td>
<td align="left" valign="top">
<p>range <a class="xref" href="mapping-types.html" title="字段数据类型">字段</a>上的范围查询支持参数<code class="literal">relation</code>，该参数可以是<code class="literal">WITHIN</code>、<code class="literal">CONTAINS</code>、<code class="literal">INTERSECTS</code>(默认)之一。</p>
</td>
</tr>
</table>
</div>
<p>该查询产生类似的结果：</p>
<div class="pre_wrapper lang-console-result">
<pre class="programlisting prettyprint lang-console-result">{
  "took": 13,
  "timed_out": false,
  "_shards" : {
    "total": 2,
    "successful": 2,
    "skipped" : 0,
    "failed": 0
  },
  "hits" : {
    "total" : {
        "value": 1,
        "relation": "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "range_index",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 1.0,
        "_source" : {
          "expected_attendees" : {
            "gte" : 10, "lte" : 20
          },
          "time_frame" : {
            "gte" : "2015-10-31 12:00:00", "lte" : "2015-11-01"
          }
        }
      }
    ]
  }
}</pre>
</div>
<div class="section">
<div class="titlepage"><div><div>
<h3 class="title">
<a id="ip-range"></a>IP 范围
</h3>
</div></div></div>
<p>除了上述范围格式，IP 范围还可以用<a href="https://en.wikipedia.org/wiki/Classless_Inter-Domain_Routing#CIDR_notation" class="ulink" target="_top">CIDR</a>符号表示：</p>
<div class="pre_wrapper lang-console">
<pre class="programlisting prettyprint lang-console">PUT range_index/_mapping
{
  "properties": {
    "ip_whitelist": {
      "type": "ip_range"
    }
  }
}

PUT range_index/_doc/2
{
  "ip_whitelist" : "192.168.0.0/16"
}</pre>
</div>
<div class="console_widget" data-snippet="snippets/695.console"></div>
</div>

<div class="section">
<div class="titlepage"><div><div>
<h3 class="title">
<a id="range-params"></a>range字段的参数
</h3>
</div></div></div>
<p>range类型接受以下参数：</p>
<div class="informaltable">
<table border="0" cellpadding="4px">
<colgroup>
<col>
<col>
</colgroup>
<tbody valign="top">
<tr>
<td valign="top">
<p>
<a class="xref" href="coerce.html" title="coerce"><code class="literal">coerce</code></a>
</p>
</td>
<td valign="top">
<p>
尝试将字符串转换为数字，并截断整数的小数部分。
接受<code class="literal">true</code>(默认) 和 <code class="literal">false</code>。
</p>
</td>
</tr>
<tr>
<td valign="top">
<p>
<a class="xref" href="mapping-boost.html" title="boost"><code class="literal">boost</code></a>
</p>
</td>
<td valign="top">
<p>
映射字段级查询时提升。接受一个浮点数，默认为<code class="literal">1.0</code>。
</p>
</td>
</tr>
<tr>
<td valign="top">
<p>
<a class="xref" href="mapping-index.html" title="index"><code class="literal">index</code></a>
</p>
</td>
<td valign="top">
<p>
该字段应该是可搜索的吗？接受<code class="literal">true</code>(默认) 和 <code class="literal">false</code>。
</p>
</td>
</tr>
<tr>
<td valign="top">
<p>
<a class="xref" href="mapping-store.html" title="store"><code class="literal">store</code></a>
</p>
</td>
<td valign="top">
<p>
字段值是否应该与<a class="xref" href="mapping-source-field.html" title="_source field"><code class="literal">_source</code></a>字段分开存储和检索。接受 <code class="literal">true</code> 或 <code class="literal">false</code>(默认)。
</p>
</td>
</tr>
</tbody>
</table>
</div>
</div>

</div>
<div class="navfooter">
<span class="prev">
<a href="percolator.html">« percolator(过滤器)数据类型</a>
</span>
<span class="next">
<a href="rank-feature.html">rank_feature(特征排名)数据类型 »</a>
</span>
</div>
</div>

                  <!-- end body -->
                        </div>
                        <div class="col-xs-12 col-sm-4 col-md-4" id="right_col">
                        
                        </div>
                    </div>
                </div>
            </section>
        </div>
    </section>
</div>
<script src="../static/cn.js"></script>
</body>
</html>